<template>
  <div>
    <el-form ref="form" :model="user" :rules="rules" label-width="80px">
      <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="user.confirmPassword" placeholder="请确认密码" type="password" />
      </el-form-item>
      <el-form-item label-width="0">
        <el-button type="primary" size="mini" @click="submit">保存</el-button>
        <el-button type="danger" size="mini" @click="close" v-show="showCloseBtn">关闭</el-button>
      </el-form-item>
    </el-form>
    
  </div>
</template>
<script>
/**
 * 修改密码
 */
export default {
    name: "superResetPwd",
    props:{
      userId: { 
        type: String,
        default:''
      },
      showCloseBtn: {
        type:Boolean,
        default:false
      }
    },
    data() {
      const equalToPassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.user.newPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      const equalToOldPassword = (rule, value, callback) => {
        if (value === this.user.oldPassword) {
          callback(new Error('新密码不能和旧密码一致!'));
        } else {
          callback();
        }
      };
      return {
        user:{
          newPassword:'',
          confirmPassword:''
        },
        rules:{
          newPassword :[
            {required: true, message: '请输入新密码', trigger: 'blur'}
          ],
          confirmPassword :[
            {required: true, message: "确认密码不能为空", trigger: "blur" },
            {validator: equalToPassword, trigger: "blur" }
          ]
        }
      };
    },
    created() {
    },
    methods: {
      submit() {
        this.$refs['form'].validate((valid) => {
            if (valid) {
              let newPassword = this.user.newPassword;
              let formObj = {
                  newPassword,
                  userId:this.userId
                }
                this.$put('/api/sys/org/sys_org_person/superResetPwd',formObj).then((res) => {
                  if(res.status===1){
                    this.$message({
                      message: '密码修改成功!',
                      type: 'success'
                    });
                    this.$refs['form'].resetFields();//清空表单
                    this.close();
                  }
                }).catch((error)=>{
                  this.$message({
                    message: '密码修改失败，请重试!',
                    type: 'success'
                  });
                  this.$refs['form'].resetFields();//清空表单
                });

            }
        });
      },
      close() {
        this.$emit('close'); 
      },
    },
    watch:{
    }
}
</script>
<style scoped>
form >>> .el-form-item__content{
  text-align: center;
  margin-left:0px;
}
</style>